<meta charset="utf-8">
<?php
// table.php?action=<view|edit>&week_number=<number>

// 判断参数是否完整
if (!isset($_GET['action']) || !isset($_GET['week_number']) || $_GET['week_number'] < 0 || $_GET['week_number'] == 0 && $_GET['action'] == 'edit') {
	die('参数错误');
}

$section_count = 12;
$day_model = array('星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日');

// 查询课程表指定周数的所有课程信息
include_once('database.php');
$sql = "SELECT day,section,name,location FROM courses ";
if ($_GET['week_number'] > 0) {
	$sql .= "WHERE week_number=${_GET['week_number']}";
}
$result = @mysqli_query($connection, $sql) or die("查询课程表失败: ".mysqli_error($connection));
$course_model = result_to_array($result);
?>

<!DOCTYPE html>
<html>
	<head>
		<style type="text/css">
			.index {
				margin-right: 50px;
				display: inline-block;
				font-size: 24px;
				line-height: 24px;
				text-decoration: none;
			}
			.index form {
				margin: 0;
				display: flex;
				align-items: center;
			}
			.index form > * {
				margin: 0 2px;
			}
			.index-warp {
				padding: 16px 0;
			}
			input.week {
				width: 30px;
				text-align: center;
			}
			table {
				width: 100%;
				border-collapse: collapse; 
			}
			table td, table th {
				border: #aaa 1px solid;
				text-align: center;
			}
			td .course-warp {
				margin: 8px;
			}
			td .course-name {
				font-weight: bold;
			}
			td a {
				display: block;
				text-decoration: none;
			}
		</style>
	</head>
<body>
	<div class="index-warp">
		<a class="index" href="?action=view&week_number=0">全部课程</a>
		<span class="index">
			<form>
				<input hidden="hidden" name="action" value="<?php echo $_GET['action'] ?>">
				<span>第</span>
				<input class="week" name="week_number" value="<?php echo $_GET['week_number'] ?>">
				<span>周</span>
				<input type="submit" value="跳转">
			</form>
		</span>
	</div>
	<table>
		<thead>
			<th></th>
			<?php foreach ($day_model as $dm): ?>
			<th><?php echo $dm ?></th>
			<?php endforeach; ?>
		</thead>
		<tbody>
			<?php for ($section=1; $section <= $section_count; $section++): ?>
			<tr>
				<td><?php echo $section; ?></td>
				<?php for ($day=1; $day <= count($day_model); $day++): ?>
				<td>
					<?php
					/// 转存指定时间的所有课程信息
					// 临时存储
					$course_temp = array();
					// 遍历所有课程信息
					foreach ($course_model as $index => $course) {
						if ($course['day'] == $day && $course['section'] == $section) {
							$course_temp[] = $course;
							// 删除已转存的课程信息，加速之后的遍历
							unset($course_model[$index]);
						}
					}
					?>
					<?php 
					if ($_GET['action'] == 'edit'): 
						/// 编辑模式时
						// 修改记录的链接
						$link = "edit.php?week_number=${_GET['week_number']}&day=$day&section=$section";
						if (count($course_temp) == 0) {
							// 此时间无课程安排
							$course_name = '+';
							$course_location = null;
						} else {
							// 此时间有课程安排
							$course_name = $course_temp[0]['name'];
							$course_location = $course_temp[0]['location'];
						}
					?>
						<div class="course-warp">
							<a class="course-name" href="<?php echo $link; ?>"><?php echo $course_name ?></a>
							<a href="<?php echo $link; ?>"><?php echo $course_location ?></a>
						</div>
					<?php
					else: 
						/// 查看模式时
						// 遍历临时存储
						foreach ($course_temp as $ct):
					?>
						<div class="course-warp">
							<div class="course-name"><?php echo $ct['name'] ?></div>
							<div><?php echo $ct['location'] ?></div>
						</div>
					<?php 
						endforeach;
					endif;
					?>
				</td>
				<?php endfor; ?>
			</tr>
			<?php endfor; ?>
		</tbody>
	</table>
	<div style="text-align:center">
		<?php if ($_GET['action'] != 'edit'): ?>
			<!-- 编辑模式的操作 -->
			<?php if ($_GET['week_number'] > 0): ?>
			<a href="?action=edit&week_number=<?php echo $_GET['week_number'] ?>"><button>增加课程表</button></a>
			<a href="?action=edit&week_number=<?php echo $_GET['week_number'] ?>"><button>修改课程表</button></a>
			<?php endif ?>
			<a href="delete.php?week_number=<?php echo $_GET['week_number'] ?>"><button>删除课程表</button></a>
		<?php else: ?>
			<!-- 查看模式的操作 -->
			<a href="?action=view&week_number=<?php echo $_GET['week_number'] ?>">完成</a>
		<?php endif ?>
	</div>
</body>
</html>